#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UTable.Input;
using UTable.Input.MultiTouch;
using UTable.Objects;
using UTable.Objects.Controls;

namespace UTable.Objects
{
    /// <summary>
    /// Interaction logic for DragObject.xaml
    /// </summary>
    public partial class DragObject : UObject
    {
        Dictionary<int, Point> prePoints = new Dictionary<int,Point>();

        public DragObject()
        {
            InitializeComponent();
            Item.InputReceived += new InputEventHandler(Item_InputReceived);
        }

        void Item_InputReceived(InputEventArgs args)
        {
            if (args is MultiTouchEventArgs)
            {
                Item_MultiTouch_Received(args as MultiTouchEventArgs);
            }
            args.Handled = true;
        }

        void Item_MultiTouch_Received(MultiTouchEventArgs args)
        {
            foreach (FingerEventArgs f in args.FingerEvents)
            {
                switch (f.EventType)
                {
                    case FingerEventType.FINGER_DOWN_EVENT:
                        fingerDown(Item, f);
                        break;
                    case FingerEventType.FINGER_IN_EVENT:
                        fingerDown(Item, f);
                        break;
                    case FingerEventType.FINGER_MOVE_EVENT:
                        fingerMove(Item, f);
                        break;
                    case FingerEventType.FINGER_OUT_EVENT:
                        fingerUp(Item, f);
                        break;
                    case FingerEventType.FINGER_UP_EVENT:
                        fingerUp(Item, f);
                        break;
                }
            }
        }

        void fingerDown(UTextBlock ob, FingerEventArgs args)
        {
            if (prePoints.ContainsKey(args.FingerID))
                return;
            Point position = ob.TranslatePoint(args.Position, Stage);
            prePoints.Add(args.FingerID, position);
            ob.CaptureFinger(args.FingerID);
            Console.WriteLine("down" + "   " + args.Position + "   " + position);
        }
        void fingerMove(UTextBlock ob, FingerEventArgs args)
        {
            if (!prePoints.ContainsKey(args.FingerID))
                return;
            // ob.Info.Fresh();
            Point position = ob.TranslatePoint(args.Position, Stage);
            Canvas.SetLeft(ob, Canvas.GetLeft(ob) + position.X - prePoints[args.FingerID].X);
            Canvas.SetTop(ob, Canvas.GetTop(ob) + position.Y - prePoints[args.FingerID].Y);
            Console.WriteLine("move" + "   " + args.Position + "   " + position + "  " + Canvas.GetLeft(ob) + ", " + Canvas.GetTop(ob));
            // ob.Info.Fresh();
            prePoints[args.FingerID] = position;
        }
        void fingerUp(UTextBlock ob, FingerEventArgs args)
        {
            if (!prePoints.ContainsKey(args.FingerID))
                return;
            ob.UncaptureFinger(args.FingerID);
            prePoints.Remove(args.FingerID);
            Console.WriteLine("up" + args.Position.ToString());
        }

        private void UButton_Pressed(object sender, ButtonEventArgs args)
        {
            if (workspace == null)
            {
                ObjectCreateParameter param = new ObjectCreateParameter(typeof(UWorkspace));
                workspace = UTableHelper.CreateObject(param);
                workspace.Owner = this.Owner;
                workspace.Closed += new EventHandler(workspace_Closed);
                workspace.ObjectLoaded += new EventHandler(workspace_ObjectLoaded);
            }
            else
            {
                workspace.Close();
            }
        }

        IObject workspace = null;

        void workspace_ObjectLoaded(object sender, EventArgs e)
        {
            Console.WriteLine("Loaded!");
        }

        void workspace_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("Closed");
        }
    }
}
